package com.cqjtu.eecs.service.impl;

import com.cqjtu.eecs.entity.TeachClassStudent;
import com.cqjtu.eecs.mapper.TeachClassStudentMapper;
import com.cqjtu.eecs.service.IStudentService;
import com.cqjtu.eecs.service.ITeachClassStudentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 教学班-学生 服务实现类
 * </p>
 *
 * @author    
 * @since 2022-03-07
 */
@Service
public class TeachClassStudentServiceImpl extends ServiceImpl<TeachClassStudentMapper, TeachClassStudent> implements ITeachClassStudentService {


    @Autowired
    private IStudentService studentService;


    /**
     * 插入教学班中所有的学生
     *
     * @param tcId 教学班编号
     * @param dataList 学生成绩excel导入信息
     * @return
     */
    @Override
    public boolean insertTeacherClassStudent(Integer tcId, List<Map<Integer, String>> dataList) {
        try {
            List<TeachClassStudent> teachClassStudents = new ArrayList<>();

            // 学生学号集合
            List<String> codeList = new ArrayList<>();
            dataList.forEach(item -> {
                codeList.add(item.get(0));
            });

            //学号id映射
            Map<String, Integer> codeIdMap = studentService.getCodeIdMap(codeList);

            // 每条学生成绩记录的size
            int size = dataList.get(0).size();
            // 教学班所有学生
            dataList.forEach(item -> {
                TeachClassStudent teachClassStudent = new TeachClassStudent(tcId, codeIdMap.get(item.get(0)), Float.parseFloat(item.get(size - 1)));
                teachClassStudents.add(teachClassStudent);
            });
            boolean saveBatch = this.saveBatch(teachClassStudents);
            if (saveBatch)
                return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
}
